﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Framework;
using Service;
using Entity.Admin;
using Web.Areas.Admin.Models;
using System.Web.Security;
using Core;

namespace Web.Areas.Admin.Controllers
{
    public class UserController : BaseController
    {
        private UserService userService;
        public UserController()
        {
            userService = new UserService(db);
        }
        //
        // GET: /Admin/Account/

        public virtual ViewResult Index(int? pageIndex, string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                var users = db.Users.OrderBy(p => p.UserName);
                return View(users.ToPagedList(pageIndex));
            }
            else
            {
                var users = db.Users.Where(p => p.UserName.Equals(keyword) || p.Comment.Equals(keyword) || p.UserName.StartsWith(keyword)).OrderBy(p => p.UserName);
                return View(users.ToPagedList(pageIndex));
            }
        }

        //
        // GET: /Admin/Account/Details/5

        public virtual ViewResult Details(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // GET: /Admin/Account/Edit/5

        public virtual ActionResult Edit(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // POST: /Admin/Account/Edit/5

        [HttpPost]
        public virtual ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    userService.CreateOrSave(user);
                }
                catch (Exception ex)
                {
                    this.AddViewMessage(ex);
                    return View(user);
                }
                //userService.CreateOrSave(user);
            }

            this.AddViewMessage("编辑成功");
            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Account/Delete/5

        public virtual ActionResult Delete(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // POST: /Admin/Account/Delete/5

        [HttpPost, ActionName("Delete")]
        public virtual ActionResult DeleteConfirmed(string id)
        {
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            this.AddViewMessage("删除成功");
            return RedirectToAction("Index", new { menuId = Request["menuId"] });
        }


        public virtual ActionResult Dashboard()
        {
            return View();
        }
        public virtual ActionResult ChannelDashboard()
        {
            return View();
        }
        public virtual ActionResult SingleLogin()
        {
            return View();
        }


        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel item)
        {
            if (ModelState.IsValid)
            {
                string userName = this.User.Identity.Name;
                try
                {
                    userService.ChangePassword(userName, item.OldPassword, item.NewPassword, item.ConfirmPassword);
                    this.AddViewMessage("修改密码成功");
                }
                catch (Exception ex)
                {
                    this.AddViewMessage(ex);
                }
            }
            return View();
        }

        public ActionResult GetUser(string term)
        {
            var userList = db.Users.Where(p => p.UserName.StartsWith(term)||p.ID==term).OrderBy(p=>p.UserName).Take(20);
            var jsonList = new List<object>();
            userList.ToList().ForEach(p => { jsonList.Add(new { id = p.ID, value = p.UserName }); });
            return Json(jsonList, JsonRequestBehavior.AllowGet);
        }

    }
}
